package com.cloud.lan.entity.base.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.lan.entity.base.PageResponse;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 基建层分页工具
 * @author qierkang
 * @since 2021-06-21
 */
@SuppressWarnings({"unchecked"})
public final class PageUtil {
    private PageUtil() {}

    /**
     * 将Mybatis Plus的分页模型转化成项目中分页模型
     * @param page
     * @param mapFunc
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T, R> PageResponse toPageResp(Page<T> page, Function<T, R> mapFunc) {
        PageResponse pageResp = new PageResponse<>();
        List<R> list = page.getRecords().stream().map(mapFunc).collect(Collectors.toList());
        pageResp.setData(list);
        com.cloud.lan.entity.base.common.Page pg = new com.cloud.lan.entity.base.common.Page();
        pg.setCurrentPage((int) page.getCurrent());
        pg.setPageSize((int) page.getSize());
        pg.setCount(page.getTotal());
        pg.setPages((int) page.getPages());
        pageResp.setPage(pg);
        return pageResp;
    }
}
